import org.testng.Assert;
import org.testng.annotations.Test;

import java.awt.*;
import java.awt.event.ActionEvent;

public class SnakeGameIntegrationTests {
    @Test
    public void testSnakeHitsWall() {
        Board board = new Board();
        Snake snake = board.getSnake();
        for (int i = 0; i < board.getB_WIDTH() / board.getDOT_SIZE() - 2; i++) {
            board.actionPerformed(new ActionEvent(this,
                    ActionEvent.ACTION_PERFORMED, null));
        }
        board.actionPerformed(new ActionEvent(this,
                ActionEvent.ACTION_PERFORMED, null));
        Assert.assertFalse(snake.isAlive(), "蛇撞墙后应该死亡。");
    }
    @Test
    public void testSnakeHitsItself() {
        Board board = new Board(10);
        Snake snake = board.getSnake();
        changeDirectionAndMove(board, Snake.Direction.DOWN);
        changeDirectionAndMove(board, Snake.Direction.LEFT);
        changeDirectionAndMove(board, Snake.Direction.UP);
        Assert.assertFalse(snake.isAlive(), "蛇撞到自身后应该死亡");
    }
        private void changeDirectionAndMove(Board board,Snake.Direction newDirection){
            board.getSnake().setCurrentDirection(newDirection);
            board.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED, null));

         }
    @Test
    public void testAppleRegeneratesAfterBeingEaten() {
        Board board = new Board();
        Apple apple = board.getApple();
        Snake snake = board.getSnake();

        Point nextPoint = (Point)snake.getHeadPosition().clone();
        nextPoint.translate(1, 0);
        apple.setPosition(nextPoint.x, nextPoint.y);

        Point oldApplePos = new Point(apple.getPosition());
        board.actionPerformed(new ActionEvent(this,
                ActionEvent.ACTION_PERFORMED, null));
        Assert.assertNotEquals(oldApplePos, apple.getPosition(), " 蛇吃掉苹果 后，苹果应该在新位置重新⽣成。");
        }
    }
